<!-- 以下代码为代码生成器生成，生成时间: 2018-03-19 22:26:09，作者：billjiang/475572229@qq.com-->
<section class="content-header">
    <h1>
        流程节点管理
        <small>列表</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="${basePath}"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li><a href="#">生产管理</a></li>
        <li class="active">流程节点管理</li>
    </ol>
</section>

<section class="content">
    <div class="row">
    
    	<div class="col-md-2">

            <!-- Profile Image -->
            <div class="box box-primary">
                <div class="box-body box-profile">
                    <div id="tree"></div>
                </div>
                <!-- /.box-body -->
            </div>
            <!-- /.box -->
        </div>
    
        <div class="col-md-4">
            <div class="box">
                <!-- /.box-header -->
                <div class="dataTables_filter" id="searchDiv">
                    <input type="hidden"  name="flowId" id="flowId" operator="like"/>
                    <div class="btn-group">
                        <button type="button" class="btn btn-default" data-btn-type="add">新增</button>
                        <button type="button" class="btn btn-default" data-btn-type="edit">编辑</button>
                        <button type="button" class="btn btn-default" data-btn-type="delete">删除</button>
                        <button type="button" class="btn btn-default" data-btn-type="sync_table">同步数据库</button>
                    </div>
                </div>
                <div class="box-body">
                    <table id="pnode_table" class="table table-bordered table-striped table-hover">
                    </table>
                </div>
            </div>
        </div>


        <div class="col-md-6">
            <div class="box">
                <!-- /.box-header -->
                <div class="dataTables_filter" id="searchFildDiv" >
                    <input type="hidden"  name="nodeId" id="nodeId" operator="like"/>
                    <div class="btn-group" style="display: none">
                        <button type="button" class="btn btn-primary" data-btn-type="search">查询</button>
                        <button type="button" class="btn btn-default" data-btn-type="reset">重置</button>
                    </div>
                    <div class="btn-group">
                        <button type="button" class="btn btn-default" data-btn-type="fadd">新增</button>
                        <button type="button" class="btn btn-default" data-btn-type="fedit">编辑</button>
                        <button type="button" class="btn btn-default" data-btn-type="fdelete">删除</button>
                    </div>
                </div>
                <div class="box-body">
                    <table id="dfiled_table" class="table table-bordered table-striped table-hover">
                    </table>
                </div>
            </div>
        </div>
        
    </div>
</section>

<script>
    var pnodeTable, filedTable,  winId = "pnodeWin";
    
    var pnodeObject = {
			loadData: function (){
				var pnode_config = {
						//定义选中行事件
						rowClick : function(row, isSelected) {
                            $("#nodeId").val(isSelected ? row.id : "-1");
                            filedTable.reloadData();
						},
						//数据加载后，默认选中第一行
						loadComplete : function() {
							pnodeTable.selectFirstRow(true);
						}
					}
				
				pnodeTable = new CommonTable("pnode_table", "pnode", "searchDiv", pnode_config);
                filedTable = new CommonTable("dfiled_table", "dform", "searchFildDiv", null);
				//初始化菜单树
				this.initTree(0);
				pnodeTable.reloadData();
			},
			
			initTree: function (selectNodeId) {
	            var self = this;
	            var treeData = null;
	            ajaxPost(basePath + "/pflow/treeData", null, function (data) {
	                treeData = data;
	            });
	            $("#tree").treeview({
	                data: treeData,
	                showBorder: true,
	                expandIcon: "glyphicon glyphicon-stop",
	                collapseIcon: "glyphicon glyphicon-unchecked",
	                levels: 1,
	                onNodeSelected: function (event, data) {
	                	$("#flowId").val(data.id);
	                	pnodeTable.reloadData();
	                }
	            });
	            if (treeData.length == 0)
	                return;
	            //默认选中第一个节点
	            selectNodeId = selectNodeId || 0;
	            $("#tree").data('treeview').selectNode(selectNodeId);
	            $("#tree").data('treeview').expandNode(selectNodeId);
	            $("#tree").data('treeview').revealNode(selectNodeId);
	        }
	}
    
    $(function (){
    	
    	pnodeObject.loadData();
    	
        //绑定按钮事件
        $("button[data-btn-type]").click(function () {
            var action = $(this).data("btn-type");
            var rowId = pnodeTable.getSelectedRowId();
            switch (action) {
                case "add":
                    var flowId = $("#flowId").val();
                    if(!flowId){
                        modals.info("请选择生产流程");
                        return false;
                    }
                    modals.openWin({
                        winId: winId,
                        title: '新增流程节点',
                        width: "900px",
                        url: basePath + "/pnode/edit?flowId="+flowId
                    })
                    break;
                case "edit":
                    if (!rowId) {
                        modals.info('请选择要编辑的行');
                        return false;
                    }
                    modals.openWin({
                        winId: winId,
                        title: '编辑流程节点【' + pnodeTable.getSelectedRowData().name + '】',
                        width: '900px',
                        url: basePath + "/pnode/edit?id=" + rowId
                    });
                    break;
                case 'delete':
                    if (!rowId) {
                        modals.info('请选择要删除的行');
                        return false;
                    }
                    modals.confirm("是否要删除该行数据？", function () {
                        ajaxPost(basePath + "/pnode/delete/" + rowId, null, function (data) {
                            if (data.success) {
                                pnodeTable.reloadRowData();
                            } else {
                                modals.info("流程节点数据被引用，不可删除！");
                            }
                        });
                    })
                    break;
                case 'sync_table':
                    if (!rowId) {
                        modals.info('请选择要同步的流程节点');
                        return false;
                    }
                    modals.confirm("是否要同步流程节点数据库表？", function () {
                        ajaxPost(basePath + "/pnode/sync_table/" + rowId, null, function (data) {
                            if (data.success) {
                            	modals.info("同步成功！");
                                pnodeTable.reloadRowData();
                            } else {
                                modals.info("同步失败！");
                            }
                        });
                    })
                    break;
               
                    
                
                case "fadd":
                    var nodeId = $("#nodeId").val();
                    if(!nodeId){
                        modals.info("请选择流程节点");
                        return false;
                    }
                    modals.openWin({
                        winId: 'dfiledWin',
                        title: '新增表单',
                        width: "900px",
                        url: basePath + "/dfiled/edit?nodeId="+nodeId
                    })
                    break;
                case "fedit":
                	var filedId = filedTable.getSelectedRowId();
                    if (!filedId) {
                        modals.info('请选择要编辑的行');
                        return false;
                    }
                    modals.openWin({
                        winId: 'dfiledWin',
                        title: '编辑表单【' + filedTable.getSelectedRowData().name + '】',
                        width: '900px',
                        url: basePath + "/dfiled/edit?id=" + filedId
                    });
                    break;
                case 'fdelete':
                	var filedId = filedTable.getSelectedRowId();
                    if (!filedId) {
                        modals.info('请选择要删除的行');
                        return false;
                    }
                    modals.confirm("是否要删除该行数据？", function () {
                        ajaxPost(basePath + "/dfiled/delete/" + filedId, null, function (data) {
                            if (data.success) {
                            	filedTable.reloadRowData();
                            } else {
                                modals.info("表单数据被引用，不可删除！");
                            }
                        });
                    })
                    break;
            }
        })
    })
    
    function renderDictType(value) {
		if (value) {
			var ret = value;
			ajaxPost(basePath + "/dict/get/" + value, null, function(data) {
				ret = data.name;
			})
			return ret;
		} else
			return value;
	}
</script>